package com.elai.web.controller.baseconfig;

import com.elai.baseconfig.domain.BaseApplicationInfoDO;
import com.elai.baseconfig.domain.BaseLesseeInfo;
import com.elai.baseconfig.domain.EventTeamDO;
import com.elai.baseconfig.domain.ImportEventItemDO;
import com.elai.baseconfig.service.BaseApplicationInfoService;
import com.elai.baseconfig.service.EventItemService;
import com.elai.baseconfig.service.EventTeamService;
import com.elai.baseconfig.service.IBaseLesseeInfoService;
import com.elai.common.core.domain.entity.SysUser;
import com.elai.common.utils.SecurityUtils;
import com.elai.common.utils.StringUtils;
import com.elai.common.annotation.Log;
import com.elai.common.core.controller.BaseController;
import com.elai.common.core.domain.AjaxResult;
import com.elai.common.core.page.TableDataInfo;
import com.elai.common.enums.BusinessType;
import com.elai.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *导入埋点事件属性
 * @author waoqi
 * @date 2021-06-18
 */
@RestController
@RequestMapping("/config/data")
public class EventDataImportController extends BaseController
{
    @Autowired
    private EventItemService eventItemService;

    @Autowired
    private BaseApplicationInfoService baseApplicationInfoService;

    @Autowired
    private EventTeamService eventTeamService;

    @Autowired
    private IBaseLesseeInfoService baseLesseeInfoService;


    @Log(title = "元数据导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport, String appCode) throws Exception
    {
        if(StringUtils.isEmpty(appCode)){
            return AjaxResult.error("请选择需要导入的应用！");
        }
        EventTeamDO app=eventTeamService.selectEventTeamById(Long.valueOf(appCode));
        if(app == null ){
            return AjaxResult.error("导入失败，事件分组不存在！");
        }
        ExcelUtil<ImportEventItemDO> util = new ExcelUtil<ImportEventItemDO>(ImportEventItemDO.class);
        //event_1默认是全埋点，其他为自定义埋点
        List<ImportEventItemDO> eventItemDOList1 = util.importExcel("event_1",file.getInputStream());
        List<ImportEventItemDO> eventItemDOList2 = util.importExcel("event_2",file.getInputStream());
        List<ImportEventItemDO> userList = util.importExcel("user",file.getInputStream());
        HashMap<String, List<ImportEventItemDO>> listHashMap=new HashMap<>();
        listHashMap.put("eventItemDOList1",eventItemDOList1);
        listHashMap.put("eventItemDOList2",eventItemDOList2);
        listHashMap.put("userList",userList);
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(StringUtils.isEmpty(currentUser.getLesseeId())) {
            return AjaxResult.error("导入数据失败，租户ID不存在");
        }
        BaseLesseeInfo lesseeInfo= baseLesseeInfoService.selectBaseLesseeInfoByLesseeId(currentUser.getLesseeId());
        if(lesseeInfo==null){
            return AjaxResult.error("导入数据失败失败，租户ID不存在");
        }
        if(userList.size()==0){
            return AjaxResult.error("共解析到0条记录，请检查excel格式是否正确");
        }
        if(userList.size()>1000){
            return AjaxResult.error("只能导入1000条以内的Excel数据");
        }
        List<ImportEventItemDO> errorDataList =eventItemService.importEventData(listHashMap,appCode,currentUser.getLesseeId());

        String msg="";
        if(errorDataList.size()==0){
            msg="导入数据成功，共导入了"+userList.size()+"条数据";
        }
        else if(errorDataList.size()==userList.size()){
            msg="导入数据失败，请检查导入模板";
        }else{
            msg="导入数据完毕，导入成功"+(userList.size()-errorDataList.size())+"条数据，导入失败"+errorDataList.size()+"条数据，请检查数据是否正确";
        }
        Map result=new HashMap();
        result.put("message",msg);
        result.put("errerList",errorDataList);
        logger.info("导入成功");
        return AjaxResult.success(result);
    }

}
